﻿<!DOCTYPE html>
<html>

<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        .wrap {
            width: 400px;
            height: 400px;
            margin: 100px auto 0;
        }

        .wrap h1 {
            text-align: center;
        }

        .wrap div {
            width: 400px;
            height: 300px;
            background: pink;
            font-size: 30px;
            text-align: center;
            line-height: 300px;
        }
    </style>

</head>

<body>

    <div class="wrap">
        <h1>按键改变颜色</h1>
        <div id="bgChange">
            keyCode为：
            <span id="keyCodeSpan"></span>
        </div>
    </div>


    <script src="./jquery-1.11.1.js"></script>
    <script>
        $(window).ready(function () {
            $(document).on('keydown', function (e) {
                console.log(e.keyCode)
                $('#keyCodeSpan').text(e.keyCode)
                switch (e.keyCode) {
                    case 82:
                        $("#bgChange").css("background", "red");
                        break;
                    case 89:
                        $("#bgChange").css("background", "yellow");
                        break;
                    case 80:
                        $("#bgChange").css("background", "purple");
                        break;
                    case 66:
                        $("#bgChange").css("background", "blue");
                        break;
                    case 83:
                        $("#bgChange").css("background", "skyblue");
                        break;
                    case 71:
                        $("#bgChange").css("background", "green");
                        break;
                    case 79:
                        $("#bgChange").css("background", "orange");
                        break;
                    default:
                        $("#keyCodeSpan").text("无此键位");
                        $("#bgChange").css("background", "pink");
                }
            })
        })



    </script>
</body>

</html>